#
# libLPShid - LPS USB Library
#
# Copyright (C) 2020-2025 Vaunix Technology Corporation
#
#  Author - NJB
#  Comments - 09-05-2025  NJB   Initial Version of Driver Code Make file
#

V ?= 0

ifeq ($(V), 0)
E = @
P = @echo 
else
E = 
P = @true
endif

TARGET_LIB = libLPShid.so
TEST_APP   = profile_test
TEST_APP_2 = LPStest

SRCDIR     = libLPShid/src
INCDIR     = -IlibLPShid/include

SRC        = $(wildcard $(SRCDIR)/*.c)
OBJS       = $(SRC:.c=.o)

TEST_SRC   = Test/profile_test.c
TEST_OBJ   = $(TEST_SRC:.c=.o)

TEST_SRC_2 = Test/LPStest.c
TEST_OBJ_2 = $(TEST_SRC_2:.c=.o)

CC         = $(CROSS_COMPILE)gcc

CFLAGS     += -g -Wall -Wextra -Wno-unused -fPIC
LDFLAGS    += -lpthread -lm -lusb-1.0

.PHONY: all clean install copylib test

all: install test

# Compile source files into objects with include directory
%.o : %.c
	$P '  CC      $(@F)'
	$E $(CC) -c $(INCDIR) $(CFLAGS) $< -o $@

# Link all object files into shared library
$(TARGET_LIB): $(OBJS)
	$P '  LD      $(@F)'
	$E $(CC) -shared -Wl,-soname,$(TARGET_LIB) $^ -o $@ $(LDFLAGS)

# Copy the library to /usr/lib/
copylib: $(TARGET_LIB)
	$E sudo cp $(TARGET_LIB) /usr/lib/
	$E sudo ldconfig

# Compile profile test app object file with include dir
$(TEST_OBJ): $(TEST_SRC)
	$P '  CC      $(@F)'
	$E $(CC) -c $(INCDIR) $(CFLAGS) $< -o $@

# Link profile test app against installed library
$(TEST_APP): $(TEST_OBJ)
	$P '  LD      $(@F)'
	$E $(CC) -o $@ $(TEST_OBJ) -lLPShid $(LDFLAGS)
	
# Compile test app object file with include dir
$(TEST_OBJ_2): $(TEST_SRC_2)
	$P '  CC      $(@F)'
	$E $(CC) -c $(INCDIR) $(CFLAGS) $< -o $@

# Link test app against installed library
$(TEST_APP_2): $(TEST_OBJ_2)
	$P '  LD      $(@F)'
	$E $(CC) -o $@ $(TEST_OBJ_2) -lLPShid $(LDFLAGS)

# Build test apps after copying library
test: copylib $(TEST_APP) $(TEST_APP_2)

clean:
	$P 'Cleaning all build files'
	$E $(RM) $(OBJS) $(TEST_OBJ) $(TEST_OBJ_2) $(TARGET_LIB) $(TEST_APP) $(TEST_APP_2)

install: copylib
